import json
import requests
from tools import config
from tools.RsaClass import rsaclass


class Mailbox:
    def __init__(self):
        self.headers = config.headers
        self.domain = config.sDomain
        self.product = config.sProduct
        self.time = config.sTime

    def listMessages(self, account_name, fids):
        ''
        list_param = {
            "fids": fids,
            "order": "receivedDate",
            "desc": True,
            "start": 0,
            "limit": 5,
            "returnTotal": True
        }
        json_param = json.dumps(list_param)
        src = "account_name=" + account_name + "&domain=" + self.domain + "&params=" + json_param + "&product=" + self.product + "&time=" + self.time
        print(src)
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "params": json_param,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/mailbox/listMessages", params=param,
                             headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())

if __name__ == '__main__':
    ops_obj = Mailbox()
    print(ops_obj.listMessages('xianfeng.yu', [1, 5]))